﻿using System;
using System.Collections.Generic;
using System.Text;
using Castle.Services.Transaction;
using MiragePWF.NH.Logging;
using MiragePWF.NH.Repositories;
using NHibernate.Criterion;
using Wolfut.MirageEE.Domain.Common;
using Wolfut.MirageEE.Domain.CommonUtilities;
using Wolfut.MirageEE.Interface.Common;
using Wolfut.MirageEE.Kernal.Common;

namespace Wolfut.MirageEE.Server.Server.Common
{
    [Transactional]
    [AutoLogging]
    public class DepartmentsService : BusinessServiceBase, IDepartmentsService
    {
        private readonly IRepository<Department> departmentRepository;
        public DepartmentsService(IRepository<Department> departmentRepository)
        {
            this.departmentRepository = departmentRepository;
        }
        #region IDepartmentsService Members

        [Transaction(TransactionMode.Requires)]
        public Department AddOrUpdateDepartment(Department department)
        {
            departmentRepository.Save(department);
            return department;
        }

        public IList<Department> listDepartment(String deptCode, String deptName, bool isDeleted)
        {
            DetachedCriteria criteria = DetachedCriteria.For(typeof(Department));
            if (!string.IsNullOrEmpty(deptCode))
            {
                criteria.Add(Expression.Eq("DeptCode", deptCode));
            }
            if (!string.IsNullOrEmpty(deptName))
            {
                criteria.Add(Expression.Like("DeptName", deptName, MatchMode.Anywhere));
            }
            criteria.Add(Expression.Eq("IsDeleted", isDeleted));
            return departmentRepository.FindAll(criteria);
        }

        #endregion

        #region 注释代码
        ///// <summary>
        ///// 增加部门信息
        ///// </summary>
        ///// <param name="coderule"></param>
        ///// <returns></returns>
        //[Transaction(TransactionMode.Requires)]
        //public Department AddDepartment(Department department)
        //{
        //    departmentRepository.Save(department);
        //    return department;
        //}
        //[Transaction(TransactionMode.Requires)]
        //public Department UpdateDepartment(Department department)
        //{
        //    departmentRepository.Save(department);
        //    return department;
        //}

        //[Transaction(TransactionMode.Requires)]
        //public void DeletedDepartment(Department department)
        //{
        //    department.IsDeleted = true;
        //    departmentRepository.Update(department);
        //}
        //[Transaction(TransactionMode.Requires)]
        //public void AllDeletedDepartment(List<Department> listDepartment)
        //{
        //    foreach (Department item in listDepartment)
        //    {
        //        item.IsDeleted = true;
        //        departmentRepository.Save(item);
        //    }
        //}

        //public Department GetDepartmentByDeptCode(String deptCode)
        //{
        //    return departmentRepository.FindFirst(Restrictions.Eq("DeptCode", deptCode));
        //}

        //public IList<Department> ListAllDepartments(Int32 firstResult, Int32 maxResults)
        //{
        //    return departmentRepository.FindAll(firstResult, maxResults);
        //}

        //public IList<Department> listAllDepartmentWithUsing()
        //{
        //    return departmentRepository.FindAll(Restrictions.Eq("IsDeleted", true));
        //}

        ///// <summary>
        ///// 按默认降序
        ///// </summary>
        ///// <param name="codeFlag"></param>
        ///// <returns></returns>
        ////public IList<CodeRule> ListCodeRulesByCodeFlag(String codeFlag)
        ////{
        ////    return null;//codeRuleRepository.FindAll(Restrictions.Eq("CodeFlag", codeFlag),Order.Desc);
        ////}

        //#region Implementation of IDepartmentsService
        //[Transaction(TransactionMode.Requires)]
        //public Department AddOrUpdateDepartment(Department department)
        //{
        //    departmentRepository.Save(department);
        //    return department;
        //}

        //public IList<Department> GetDepartment(string deptCode, string deptName, bool isDeleted)
        //{
        //    DetachedCriteria criteria = DetachedCriteria.For(typeof (Department));
        //    if(!string.IsNullOrEmpty(deptCode))
        //    {
        //        criteria.Add(Expression.Eq("DeptCode", deptCode));
        //    }
        //    if(!string.IsNullOrEmpty(deptName))
        //    {
        //        criteria.Add(Expression.Like("DeptName", deptName, MatchMode.Anywhere));
        //    }
        //    criteria.Add(Expression.Eq("IsDeleted", isDeleted));
        //    return departmentRepository.FindAll(criteria);
        //}

        //#endregion

        //#region IDepartmentsService Members


        //public IList<Department> listDepartment(bool isDeleted)
        //{
        //    throw new NotImplementedException();
        //}

        //#endregion        
        #endregion
    }
}